package cn.hill4j.tool.spring.ext.mvc;

import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
/**
  * ClassName: WrapMethodReturnValueHandler
  * Description: 响应结果转换包装执行器
  * @author hillchen
  * Date: 2023/8/13
  * @version: 1.0
  */
public class WrapMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
    private final HandlerMethodReturnValueHandler targetHandler ;
    /**
     * @author hillchen
     * Description: 构造方法
     * Date: 2023/8/13 00:50
     * @param targetHandler 响应结果转换处理器
     **/
    public WrapMethodReturnValueHandler(HandlerMethodReturnValueHandler targetHandler) {
        this.targetHandler = targetHandler;
    }

    @Override
    public boolean supportsReturnType(MethodParameter returnType) {
        return targetHandler.supportsReturnType(returnType);
    }

    @Override
    public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
        targetHandler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
    }
    /**
     * @author hillchen
     * Description: 包装响应结果转换器
     * Date: 2023/8/13 00:50
     * @param targetHandler 响应结果转换器
     * @return HandlerMethodReturnValueHandler
     **/
    public static HandlerMethodReturnValueHandler wrap(HandlerMethodReturnValueHandler targetHandler){
        return new WrapMethodReturnValueHandler(targetHandler);
    }
}
